Page({

  /**
   * 页面的初始数据
   */
  data: {
    mycarList:[]
  },
  //绑定车辆
  bind(){
    const that = this;
    let bluetoothName= wx.getStorageSync('bluetoothName')
    let blueView= wx.getStorageSync('blueView')
    if(blueView){
      wx.showToast({
      title: '您当前蓝牙未链接车辆',
      icon: 'error',
      })
    }else{
      wx.showModal({
        title: '操作确认',
        content: `您要绑定${bluetoothName}吗？`,
        success (res) {
          if (res.confirm) {
            let token =  wx.getStorageSync('token')
            let motorSn =  wx.getStorageSync('motorSn')
            // let bluetoothName= wx.getStorageSync('bluetoothName')
            console.log('(motorSn=)' + motorSn + '(bluetoothName=)'+bluetoothName);
            wx.request({
              url: 'https://api.gowow.cn/moto/bind',
              data: {
                "motorSn": motorSn,
                "bluetoothName": bluetoothName,
              },
              method:'POST',
              header:{
                Authorization: token
              },
              success (res) {
                // console.log(res);
                wx.showToast({
                  title: res.data.msg,
                });
                that.onLoad()
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });
      
    }
  },
  //解绑车辆
  unbind(e){
    const that = this;
    var param = e.currentTarget.dataset.param;
    console.log( e.currentTarget.dataset.param);
    wx.showModal({
      title: '操作确认',
      content: `您要解绑${param.bluetoothName}吗？`,
      success (res) {      
        if (res.confirm) {
          let token =  wx.getStorageSync('token')
          wx.request({
            url: 'https://api.gowow.cn/moto/unbind',
            data: {
              "motoSn": param.motoSn,
            },
            method:'POST',
            header:{
              Authorization: token
            },
            success (res) {
              wx.showToast({
                title: res.data.msg,
              });
              that.onLoad()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  //退出登录
  logout(){
    wx.showModal({
      title: '操作确认',
      content: `您要确定退出登录吗？`,
      success (res) {
        if (res.confirm) {
          let token =  wx.getStorageSync('token')
          wx.request({
            url: 'https://api.gowow.cn/user/logout',
            method:'POST',
            header:{
              Authorization: token
            },
            success (res) {
              // console.log(res);
              wx.showToast({
                title: res.data.msg,
              });
              wx.redirectTo({
                url: `/pages/login/index`,
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  //获取车辆列表
  getcarList(){
    const that = this;
    let token =  wx.getStorageSync('token')
    wx.request({
      url: 'https://api.gowow.cn/moto/my',
      method:'GET',
      header:{
        Authorization: token
      },
      success (res) {
        // console.log(res.data.data);
        that.setData({
          mycarList : res.data.data
        })
        console.log("个人中心页面车辆列表");
        console.log(that.data.mycarList);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getcarList()
  }
})

